using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using T2.Logging.Business;
using T2.Logging.Business.Entities.Interfaces;
using T2.Logging.Business.Entities;
using T2.Logging.Business.Entities.Search;
using T2.Logging.Business.Entities.Search.Interfaces;
namespace T2.WebSecurity.Common.Log
{
    
    
    public static partial class MyLogger
    {

        static BusinessService bs = new BusinessService();
        public static void MyLog(string _ApplicationName, string _Caller, string _Message, MyEnum.LogCategories _Category, int _Priority, int _Event, TraceEventType _TraceType, string _Title)
        {
            
            //This is where you hook in the Logger
            ILog_biz log = new Log_biz();
            log.Action = MyEnum.Actions.Add.ToString();
            log.AppDomainName = _ApplicationName;
            log.ApplicationID = GetApplicationIDByName(_ApplicationName);
            log.CreatedBy = _Caller;
            log.CreatedDt = DateTime.Now;
            log.ModifiedBy = _Caller;
            log.ModifiedDt = DateTime.Now;
            log.LogCategoryID = GetCategoryIDByName(_Category.ToString());
            log.MachineName = "";
            log.Message = _Message;
            log.Priority = _Priority;
            log.Title = _Title;
            log.Severity = _TraceType.ToString();

            IDictionary<string, int> retDict = bs.LogController_Set(log);


            

        
        }

        private static Int32 GetApplicationIDByName(string _name)
        {
            IApplicationSearch_biz app = new ApplicationSearch_biz();
            app.ApplicationName = _name;
            app.PageNumber = 1;
            app.PageSize = 1;
            app.SortColumn = "ApplicationID";
            app.SortType = "asc";
            List<IApplication_biz> retList = bs.ApplicationController_Search(app,false, false);
            if (retList != null && retList.Count > 0)
            {
                return retList.FirstOrDefault().ApplicationID;
            }
            return -1;
            
        }

        private static Int32 GetCategoryIDByName(string _name)
        {
            ILogCategorySearch_biz param = new LogCategorySearch_biz();
            param.LogCategoryName= _name;
            param.PageNumber = 1;
            param.PageSize = 1;
            param.SortColumn = "CategoryID";
            param.SortType = "asc";
            List<ILogCategory_biz> retList = bs.LogCategoryController_Search(param, false, false);
            if (retList != null && retList.Count > 0)
            {
                return retList.FirstOrDefault().LogCategoryID;
            }
            return -1;

        }

        
       
    }
}